package com.fwmagic.spark.core.transformations

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.rdd.RDD

object MapPartitionsWithIndexDemo {
    def main(args: Array[String]): Unit = {
        val conf: SparkConf = new SparkConf()
                .setAppName(this.getClass.getSimpleName)
                .setMaster("local[*]")

        val sc: SparkContext = new SparkContext(conf)

        val rdd1: RDD[Int] = sc.parallelize(Array(1,2,3,4,5,6,7,8,9),3)

        val rdd2: RDD[(Int, Int)] = rdd1.mapPartitionsWithIndex((index, iter) => {
            iter.map(ele => (index, ele))
        })

        rdd2.collect().foreach(println)

        sc.stop()
    }
}
